#!/usr/bin/python
import sys
import os

# Parameters.
in_file = sys.argv[1]
out_dir = sys.argv[2]

# Setup output files.
fouts = {}

# Open input file.
fin = open(in_file,"rb")
for line in fin:
	# Tokenize.
	tmp = line.strip().split("\t")
	outf = tmp[0]
	ctg = tmp[1]
	rna = tmp[3]
	start = int(tmp[4])
	stop = (tmp[5])
	
	if start > stop:
		t = start
		start = stop
		stop = t
	
	# Make sure output is open.
	if outf not in fouts:
		t = "%s/%s.gff" % (out_dir, outf)
		fouts[outf] = open(t,"w")
	
	# Write out gff.
	gff = ctg
	gff += "\t" + "PITA"
	gff += "\t" + rna
	gff += "\t" + str(start)
	gff += "\t" + str(stop)
	gff += "\t" + "."
	gff += "\t" + "."
	gff += "\t" + "."
	gff += "\t" + "group"
	fouts[outf].write(gff + "\n")
	
fin.close()

for f in fouts:
	fouts[f].close()
